package msdark.cliente;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;



public class cliente extends Activity {
	
	/*variables*/
	Button btnConnect;
	Button btnClose;
	EditText ipServer;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
            /*try{
        	startClient();
        	
        }catch(Exception e){
        	e.printStackTrace();
        }*/
        
        String cachedir = this.getCacheDir().getAbsolutePath();
        setContentView(R.layout.main);
        btnConnect = (Button)this.findViewById(R.id.btnConectar);
        btnClose   = (Button)this.findViewById(R.id.btnClose);
        ipServer   = (EditText)this.findViewById(R.id.ip_servidor);
        ConnectListener listener = new ConnectListener(this,cachedir);
        btnConnect.setOnClickListener(listener);
        
        btnClose.setOnClickListener(new View.OnClickListener(){
        	@Override 
        	public void onClick(View v){
        		btnConnect.setEnabled(false);
        		finish();
        	}
        });
    }
    
    private class ConnectListener implements View.OnClickListener{
    	private String cachedir;
    	private Context context;
    	public ConnectListener(Context cont,String dir){
    		super();
    		cachedir = dir;
    		context = cont;
    		
    	}
    	@Override
		public void onClick(View v) {
    		btnConnect.setText("Conectando...");
			btnConnect.setEnabled(false);
			btnClose.setEnabled(false);
			try{
				android.util.Log.d("IPSERVER",ipServer.getText().toString());
				Coneccion coneccion = Coneccion.getInstance();
				boolean result = coneccion.startClient(context, cachedir, ipServer.getText().toString());
				if(result==false){
					AlertDialog.Builder builder = new AlertDialog.Builder(context);
					builder.setMessage("Imposible conectar")
					       .setCancelable(false)
					       .setPositiveButton("Intentar nuevamente", new DialogInterface.OnClickListener() {
					           public void onClick(DialogInterface dialog, int id) {
					                dialog.dismiss();
					           }
					       });
					AlertDialog alert = builder.create();
					alert.show();
					btnConnect.setEnabled(true);
					btnClose.setEnabled(true);
				}else{
					AlertDialog.Builder builder = new AlertDialog.Builder(context);
					builder.setMessage("Coneccion realizada")
					       .setCancelable(false)
					       .setPositiveButton("continuar", new DialogInterface.OnClickListener() {
					           public void onClick(DialogInterface dialog, int id) {
					                dialog.dismiss();
					                Intent i = new Intent(cliente.this, ViewListado.class);
									startActivity(i);
									btnConnect.setEnabled(true);
									btnClose.setEnabled(true);
					           }
					       });
					AlertDialog alert = builder.create();
					alert.show();
					
				}
			}catch(Exception e){
				AlertDialog.Builder builder = new AlertDialog.Builder(context);
				builder.setMessage(e.getMessage())
				       .setCancelable(false)
				       .setPositiveButton("Intentar nuevamente", new DialogInterface.OnClickListener() {
				           public void onClick(DialogInterface dialog, int id) {
				                cliente.this.finish();
				           }
				       });
				AlertDialog alert = builder.create();
				alert.show();
				btnConnect.setEnabled(true);
				btnClose.setEnabled(true);
			}
		}
    }
    
}